package com.zds.support.manager

import android.text.TextUtils
import com.zds.support.constant.KeyConstants
import com.zds.support.util.SharedPreferenceUtils

/**
 *
 *@Description
 *@Author zc
 *@Version V1.0
 **/
class UserDataManager {
    //跟随账号
    private val userData by lazy {
        SharedPreferenceUtils(SP_USER_DATA_NAME)
    }
    private val videoCacheData by lazy {
        SharedPreferenceUtils(SP_VIDEO_CACHE_DATA_NAME)
    }

    //不跟随账号
    private val appData by lazy {
        SharedPreferenceUtils(SP_APP_DATA_NAME)
    }

    private var userToken = ""
    private var pairBikeMac = ""
    private var pairHeartRateMac = ""
    private var userId = ""
    private var siteId = 0
    private var userNickName = ""
    companion object {
        private const val SP_USER_DATA_NAME = "sp_user_data"
        private const val SP_APP_DATA_NAME = "sp_app_data"
        private const val SP_VIDEO_CACHE_DATA_NAME = "sp_video_cache_data_name"

        @JvmStatic
        val instance: UserDataManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            UserDataManager()
        }
    }

    private constructor() {
        userToken = appData.getString(KeyConstants.KEY_USER_TOKEN, "")
        userNickName = userData.getString(KeyConstants.KEY_USER_NICK_NAME, "")
    }

    fun getToken(): String {
        if (TextUtils.isEmpty(userToken)) {

        }

        return userToken
    }

    fun setToken(token: String) {
        userToken = token
        appData.saveString(KeyConstants.KEY_USER_TOKEN, token)
    }
    fun setSiteId(site: Int){
        siteId = site
        userData.saveInt(KeyConstants.KEY_SITE_ID,site)
    }

    fun setLogin(isLogin: Boolean) {
        userData.saveBoolean(KeyConstants.KEY_USER_IS_LOGIN, isLogin)
    }

    fun isLogin(): Boolean {
        return userData.getBoolean(KeyConstants.KEY_USER_IS_LOGIN, false)
    }

    private fun clearAppSharedPreferences() {
        userData.clear()
        userNickName = ""
    }

    fun getUserBirthDay(): Long {
        return userData.getLong("key_user_birthDay", 631123200000L)
    }


    fun setUserBirthDay(milliSeconds: Long) {
        userData.saveLong("key_user_birthDay", milliSeconds)
    }

    fun setUserIsMan(isMan: Boolean) {
        userData.saveBoolean("key_user_is_man", isMan)
    }
    fun setUserNickName(nickName: String?) {
        var nickname = ""
        if (nickName != null) {
            nickname = nickName
        }
        userNickName = nickname;
        userData.saveString(KeyConstants.KEY_USER_NICK_NAME, nickname)
    }

    fun setUid(uid: Int) {
        setUid(uid.toString())
    }

    fun setUid(uid: String) {
        userId = uid
        userData.saveString(KeyConstants.KEY_USER_ID, uid)
    }

    fun getUid(): String {
        return userData.getString(KeyConstants.KEY_USER_ID)
    }

    fun getSiteId(): Int{
        return userData.getInt(KeyConstants.KEY_SITE_ID,0)
    }


    /**
     * 获取当前用户的昵称
     *
     * @return
     */
    fun getUserNickName(): String {
        return userData.getString(KeyConstants.KEY_USER_NICK_NAME)
        //return userNickName
    }
    fun getUserIsMan(): Boolean {
        return userData.getBoolean("key_user_is_man", false)
    }
    fun setJt(jt: Boolean) {
        appData.saveBoolean(KeyConstants.KEY_ZDJT, jt)
    }

    fun isJt(): Boolean {
        return appData.getBoolean(KeyConstants.KEY_ZDJT,false)
    }
    fun logout() {
        CacheManager.instance.clearUserInfo()
        setToken("")
        clearAppSharedPreferences()
        setLogin(false)
    }
}